﻿using Kimd.Common;
using Kimd.Communication;
using Kimd.Logic;
using Kimd.Machine;
using Kimd.Motion;
using Kimd.Vision;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using WorkStation.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace WorkStation.Logic
{
    //名称： 精定位移载1  精定位移载2
    //moduleIndex: 0 - 精定位移载1  1 - 精定位移载2
    public class PrecisionLogic : FlowBase
    {
        private int _moduleIndex;
        private Kimd.Motion.Axis _ngAxis;
        private Cylinder _scanUpdownCylinder;
        private Cylinder _scanCylinder;
        private Cylinder _upMoveCylinder;
        private Cylinder _downMoveCylinder;

        private List<Cylinder> _upFeedPositionCylinder;
        private List<Cylinder> _downFeedPositionCylinder;
        private List<Cylinder> _upRecyclePositionCylinder;
        private List<Cylinder> _downRecyclePositionCylinder;

        private Cylinder _separateCylinderUp;
        private Cylinder _separateCylinderDown;

        private Cylinder _upRevacuumCylinder;
        private Cylinder _downRevacuumCylinder;

        //private List<Cylinder> _upVacs;
        //private List<Cylinder> _upFeedVacs;
        //private List<Cylinder> _upRecycleVacs;
        //private List<Cylinder> _downVacs;
        //private List<Cylinder> _downFeedVacs;
        //private List<Cylinder> _downRecycleVacs;
        private List<Cylinder> _buffer1Vacs;
        private List<Cylinder> _buffer2Vacs;
        private Sensor _scanNG1InPosition;
        private Sensor _scanNG2InPosition;
        private Sensor _aNGInPosition;
        private Sensor _abNGInPosition;
        private Sensor _abbNGInPosition;
        //左边头用的是1，右边头用的是2
        private Tray _feedTray1Up;
        private Tray _feedTray2Up;
        private Tray _recycleTray1Up;
        private Tray _recycleTray2Up;
        private Tray _feedTray1Down;
        private Tray _feedTray2Down;
        private Tray _recycleTray1Down;
        private Tray _recycleTray2Down;

        //**********************************
        //feed1 对应的是barcodescanner1
        public CommunicationHelper BarcodeScanner1;
        public CommunicationHelper BarcodeScanner2;
        public CommunicationHelper BarcodeScanner3;
        public CommunicationHelper BarcodeScanner4;

        private bool _currentLayerStatus;
        private bool _currentLayerStatusRobot;

        public HeaderPosition UpHeaderPos;
        private object scanLockObj;
        public PrecisionLogic(string flowName, FlowManager flowmanager, int moduleIndex)
            : base(flowName, flowmanager)
        {
            _moduleIndex = moduleIndex;

            if (moduleIndex == 0)
            {
                scanLockObj = new object();
                _ngAxis = MachineResource.Instance.MachineAxis["精定位1NG轴"];
                _scanUpdownCylinder = MachineResource.Instance.MachineCylinder["精定位1扫码升降气缸"];
                _scanCylinder = MachineResource.Instance.MachineCylinder["精定位1扫码前后气缸"];
                _upMoveCylinder = MachineResource.Instance.MachineCylinder["精定位1上层移载气缸"];
                _downMoveCylinder = MachineResource.Instance.MachineCylinder["精定位1下层移载气缸"];



                _upFeedPositionCylinder = new List<Cylinder>()
                {
                    MachineResource.Instance.MachineCylinder["精定位1上层定位气缸2-2"],
                    MachineResource.Instance.MachineCylinder["精定位1上层定位气缸1-2"]
                };

                _downFeedPositionCylinder = new List<Cylinder>()
                {
                    MachineResource.Instance.MachineCylinder["精定位1下层定位气缸2-2"],
                    MachineResource.Instance.MachineCylinder["精定位1下层定位气缸1-2"]
                };

                _upRecyclePositionCylinder = new List<Cylinder>()
                {
                    MachineResource.Instance.MachineCylinder["精定位1上层定位气缸2"],
                    MachineResource.Instance.MachineCylinder["精定位1上层定位气缸1"]
                };

                _downRecyclePositionCylinder = new List<Cylinder>()
                {
                    MachineResource.Instance.MachineCylinder["精定位1下层定位气缸2"],
                    MachineResource.Instance.MachineCylinder["精定位1下层定位气缸1"]
                };


                _separateCylinderUp = MachineResource.Instance.MachineCylinder["精定位1上层错位气缸"];
                _separateCylinderDown = MachineResource.Instance.MachineCylinder["精定位1下层错位气缸"];

                _upRevacuumCylinder = MachineResource.Instance.MachineCylinder[$"精定位{_moduleIndex + 1}上层吹气"];
                _downRevacuumCylinder = MachineResource.Instance.MachineCylinder[$"精定位{_moduleIndex + 1}下层吹气"];
                //_upVacs = new List<Cylinder>()
                //{
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸1"],
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸2"],
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸3"],
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸4"],
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸5"],
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸6"],
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸7"],
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸8"],
                //};
                //_upFeedVacs = new List<Cylinder>()
                //{
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸1"],
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸2"],
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸5"],
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸6"],
                //};
                //_upRecycleVacs = new List<Cylinder>()
                //{
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸3"],
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸4"],
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸7"],
                //    MachineResource.Instance.MachineCylinder["精定位1上层真空吸8"],
                //};
                //_downVacs = new List<Cylinder>()
                //{
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸1"],
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸2"],
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸3"],
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸4"],
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸5"],
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸6"],
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸7"],
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸8"],
                //};
                //_downFeedVacs = new List<Cylinder>()
                //{
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸1"],
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸2"],
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸5"],
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸6"],
                //};
                //_downRecycleVacs = new List<Cylinder>()
                //{
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸3"],
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸4"],
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸7"],
                //    MachineResource.Instance.MachineCylinder["精定位1下层真空吸8"],
                //};
                _buffer1Vacs = new List<Cylinder>()
                {
                    MachineResource.Instance.MachineCylinder["精定位1OKBuffer位真空吸1"],
                    MachineResource.Instance.MachineCylinder["精定位1OKBuffer位真空吸2"],
                };
                _buffer2Vacs = new List<Cylinder>()
                {
                    MachineResource.Instance.MachineCylinder["精定位1OKBuffer位真空吸3"],
                    MachineResource.Instance.MachineCylinder["精定位1OKBuffer位真空吸4"],
                };

                _scanNG1InPosition = MachineResource.Instance.MachineSensor["精定位1扫码NG1盘在原点位"];
                _scanNG2InPosition = MachineResource.Instance.MachineSensor["精定位1扫码NG2盘在原点位"];
                _aNGInPosition = MachineResource.Instance.MachineSensor["精定位1ANG盘在原点位"];
                _abNGInPosition = MachineResource.Instance.MachineSensor["精定位1ABNG盘在原点位"];
                _abbNGInPosition = MachineResource.Instance.MachineSensor["精定位1ABBNG盘在原点位"];

                BarcodeScanner1 = new CommunicationHelper("精定位扫码枪1", MachineResource.Instance.MachineTcpClient["精定位扫码枪1"]);
                BarcodeScanner2 = new CommunicationHelper("精定位扫码枪2", MachineResource.Instance.MachineTcpClient["精定位扫码枪2"]);
                BarcodeScanner3 = new CommunicationHelper("精定位扫码枪3", MachineResource.Instance.MachineTcpClient["精定位扫码枪3"]);
                BarcodeScanner4 = new CommunicationHelper("精定位扫码枪4", MachineResource.Instance.MachineTcpClient["精定位扫码枪4"]);

                RegisterDirectSignal("Arm1精定位到位");
                RegisterDirectSignal("Arm2精定位到位");
            }
            else
            {
                scanLockObj = new object();
                _ngAxis = MachineResource.Instance.MachineAxis["精定位2NG轴"];
                _scanUpdownCylinder = MachineResource.Instance.MachineCylinder["精定位2扫码升降气缸"];
                _scanCylinder = MachineResource.Instance.MachineCylinder["精定位2扫码前后气缸"];
                _upMoveCylinder = MachineResource.Instance.MachineCylinder["精定位2上层移载气缸"];
                _downMoveCylinder = MachineResource.Instance.MachineCylinder["精定位2下层移载气缸"];



                _upFeedPositionCylinder = new List<Cylinder>()
                {
                    MachineResource.Instance.MachineCylinder["精定位2上层定位气缸1-2"],
                    MachineResource.Instance.MachineCylinder["精定位2上层定位气缸2-2"]
                };

                _downFeedPositionCylinder = new List<Cylinder>()
                {
                    MachineResource.Instance.MachineCylinder["精定位2下层定位气缸1-2"],
                    MachineResource.Instance.MachineCylinder["精定位2下层定位气缸2-2"]
                };

                _upRecyclePositionCylinder = new List<Cylinder>()
                {
                    MachineResource.Instance.MachineCylinder["精定位2上层定位气缸1"],
                    MachineResource.Instance.MachineCylinder["精定位2上层定位气缸2"]
                };

                _downRecyclePositionCylinder = new List<Cylinder>()
                {
                    MachineResource.Instance.MachineCylinder["精定位2下层定位气缸1"],
                    MachineResource.Instance.MachineCylinder["精定位2下层定位气缸2"]
                };


                _separateCylinderUp = MachineResource.Instance.MachineCylinder["精定位2上层错位气缸"];
                _separateCylinderDown = MachineResource.Instance.MachineCylinder["精定位2下层错位气缸"];

                _upRevacuumCylinder = MachineResource.Instance.MachineCylinder[$"精定位{_moduleIndex + 1}上层吹气"];
                _downRevacuumCylinder = MachineResource.Instance.MachineCylinder[$"精定位{_moduleIndex + 1}下层吹气"];
                //_upVacs = new List<Cylinder>()
                //{
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸1"],
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸2"],
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸3"],
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸4"],
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸5"],
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸6"],
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸7"],
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸8"],
                //};
                //_upFeedVacs = new List<Cylinder>()
                //{
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸1"],
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸2"],
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸5"],
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸6"],
                //};
                //_upRecycleVacs = new List<Cylinder>()
                //{
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸3"],
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸4"],
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸7"],
                //    MachineResource.Instance.MachineCylinder["精定位2上层真空吸8"],
                //};
                //_downVacs = new List<Cylinder>()
                //{
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸1"],
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸2"],
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸3"],
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸4"],
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸5"],
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸6"],
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸7"],
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸8"],
                //};
                //_downFeedVacs = new List<Cylinder>()
                //{
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸1"],
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸2"],
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸5"],
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸6"],
                //};
                //_downRecycleVacs = new List<Cylinder>()
                //{
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸3"],
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸4"],
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸7"],
                //    MachineResource.Instance.MachineCylinder["精定位2下层真空吸8"],
                //};
                _buffer1Vacs = new List<Cylinder>()
                {
                    MachineResource.Instance.MachineCylinder["精定位2OKBuffer位真空吸1"],
                    MachineResource.Instance.MachineCylinder["精定位2OKBuffer位真空吸2"],
                };
                _buffer2Vacs = new List<Cylinder>()
                {
                    MachineResource.Instance.MachineCylinder["精定位2OKBuffer位真空吸3"],
                    MachineResource.Instance.MachineCylinder["精定位2OKBuffer位真空吸4"],
                };

                _scanNG1InPosition = MachineResource.Instance.MachineSensor["精定位2扫码NG1盘在原点位"];
                _scanNG2InPosition = MachineResource.Instance.MachineSensor["精定位2扫码NG2盘在原点位"];
                _aNGInPosition = MachineResource.Instance.MachineSensor["精定位2ANG盘在原点位"];
                _abNGInPosition = MachineResource.Instance.MachineSensor["精定位2ABNG盘在原点位"];
                _abbNGInPosition = MachineResource.Instance.MachineSensor["精定位2ABBNG盘在原点位"];

                BarcodeScanner1 = new CommunicationHelper("精定位扫码枪5", MachineResource.Instance.MachineTcpClient["精定位扫码枪5"]);
                BarcodeScanner2 = new CommunicationHelper("精定位扫码枪6", MachineResource.Instance.MachineTcpClient["精定位扫码枪6"]);
                BarcodeScanner3 = new CommunicationHelper("精定位扫码枪7", MachineResource.Instance.MachineTcpClient["精定位扫码枪7"]);
                BarcodeScanner4 = new CommunicationHelper("精定位扫码枪8", MachineResource.Instance.MachineTcpClient["精定位扫码枪8"]);

                RegisterDirectSignal("Arm3精定位到位");
                RegisterDirectSignal("Arm4精定位到位");
            }

            _feedTray1Up = new Tray(2, 1);
            _feedTray2Up = new Tray(2, 1);
            _recycleTray1Up = new Tray(2, 1);
            _recycleTray2Up = new Tray(2, 1);
            _feedTray1Down = new Tray(2, 1);
            _feedTray2Down = new Tray(2, 1);
            _recycleTray1Down = new Tray(2, 1);
            _recycleTray2Down = new Tray(2, 1);

            RegisterDirectSignal("机械手可放料");
            RegisterDirectSignal("清空Buffer");

            //_upVacs.ForEach(v => v.Off());
            //_downVacs.ForEach(v => v.Off());
        }

        public override void InSingleRunMode()
        {
            if (GlobalVariable.OnSingleRunPause != null)
                GlobalVariable.OnSingleRunPause();
        }



        public override void Calibrate()
        {
            base.Calibrate();
        }

        public override void Home()
        {
            //等待arm回零完成
            if (_moduleIndex == 0)
            {
                if (!SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                {
                    WaitForSignal("Robot1模组", "回零完成");
                    if (!CheckFlowCondition()) return;
                }
                WaitForSignal("Arm1模组", "回零完成");
                if (!CheckFlowCondition()) return;
                WaitForSignal("Arm2模组", "回零完成");
                if (!CheckFlowCondition()) return;
            }
            else
            {
                if (!SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                {
                    WaitForSignal("Robot2模组", "回零完成");
                    if (!CheckFlowCondition()) return;
                }
                WaitForSignal("Arm3模组", "回零完成");
                if (!CheckFlowCondition()) return;
                WaitForSignal("Arm4模组", "回零完成");
                if (!CheckFlowCondition()) return;
            }
            while (_ngAxis.ServoWarn && !IsBreak())
            {
                ShowWarnAndPause($"精定位{_moduleIndex + 1} NG轴报警中...", _ngAxis.Module);
                Thread.Sleep(20);
            }
            _ngAxis.SetServoOn();
            Thread.Sleep(1000);
            SingleAxisGoHome(_ngAxis);
            if (!CheckFlowCondition()) return;
            _scanUpdownCylinder.Off();
            Thread.Sleep(1000);
            //SingleCylinderOperate(_scanUpdownCylinder, 5000, false);
            if (!CheckFlowCondition()) return;
            //_scanCylinder.Off();
            //Thread.Sleep(500);
            SingleCylinderOperate(_scanCylinder, 5000, false);
            if (!CheckFlowCondition()) return;


            MultiCylinderOperate(_upFeedPositionCylinder, 5000, new List<bool>() { false, false });
            if (!CheckFlowCondition()) return;
            MultiCylinderOperate(_downFeedPositionCylinder, 5000, new List<bool>() { false, false });
            if (!CheckFlowCondition()) return;
            MultiCylinderOperate(_downRecyclePositionCylinder, 5000, new List<bool>() { false, false });
            if (!CheckFlowCondition()) return;
            MultiCylinderOperate(_upRecyclePositionCylinder, 5000, new List<bool>() { false, false });
            if (!CheckFlowCondition()) return;


            //_upVacs.ForEach(v => v.Off());
            //_downVacs.ForEach(v => v.Off());
            _buffer1Vacs.ForEach(v => v.Off());
            _buffer2Vacs.ForEach(v => v.Off());
            //把上层送到机械手那里
            SwitchOnce(false, _moduleIndex);
            if (!CheckFlowCondition()) return;
            //shutdown vacs
            if (!GlobalVariable.IsDryRun)
            {
                while (!_scanNG1InPosition.WaitOn() && !IsBreak())
                {
                    ShowWarnAndPause("扫码NG1盘不在位，请补充!", _scanNG1InPosition.Module, true);
                    Thread.Sleep(20);
                }
                if (!CheckFlowCondition()) return;
                while (!_scanNG2InPosition.WaitOn() && !IsBreak())
                {
                    ShowWarnAndPause("扫码NG2盘不在位，请补充!", _scanNG2InPosition.Module, true);
                    Thread.Sleep(20);
                }
                if (!CheckFlowCondition()) return;
                //while (!_aNGInPosition.WaitOn() && !IsBreak())
                //{
                //    ShowWarnAndPause("A NG盘不在位，请补充!", _aNGInPosition.Module, true);
                //    Thread.Sleep(20);
                //}
                if (!CheckFlowCondition()) return;
                while (!_abNGInPosition.WaitOn() && !IsBreak())
                {
                    ShowWarnAndPause("AB NG盘不在位，请补充!", _abNGInPosition.Module, true);
                    Thread.Sleep(20);
                }
                if (!CheckFlowCondition()) return;
                while (!_abbNGInPosition.WaitOn() && !IsBreak())
                {
                    ShowWarnAndPause("ABB NG盘不在位，请补充!", _abbNGInPosition.Module, true);
                    Thread.Sleep(20);
                }
                if (!CheckFlowCondition()) return;
            }

            _upRevacuumCylinder.Off();
            _downRevacuumCylinder.Off();

            _feedTray1Up.Helper.NewEmptyTray();
            _feedTray2Up.Helper.NewEmptyTray();
            _recycleTray1Up.Helper.NewEmptyTray();
            _recycleTray2Up.Helper.NewEmptyTray();

            _feedTray1Down.Helper.NewEmptyTray();
            _feedTray2Down.Helper.NewEmptyTray();
            _recycleTray1Down.Helper.NewEmptyTray();
            _recycleTray2Down.Helper.NewEmptyTray();

            BarcodeScanner1.EmptySendBuffer();
            BarcodeScanner2.EmptySendBuffer();
            GlobalVariable.FrontClearFinish = false;
            GlobalVariable.BackClearFinish = false;
            if (_moduleIndex == 0)
            {
                GlobalVariable.IsClearDownTray = false;
            }
            else
            {
                GlobalVariable.IsClearDownTray2 = false;
            }
            scanCounttttt = 0;
            scanCount2 = 0;
        }

        public override void Init(out string errInfo)
        {
            base.Init(out errInfo);
        }
        public override void Method()
        {
            if (!CheckFlowCondition()) return;
            if (GlobalVariable.IsSpotChecking)
                return;
            upstart:
            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"交换上下层,上层至机械手侧", this.Description, 0, _scanNG1InPosition.Module));
            SwitchOnce(isDownInRobotFeed: false, _moduleIndex);
            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"交换上下层完成", this.Description, 0, _scanNG1InPosition.Module));
            if (!CheckFlowCondition()) return;
            //机械手侧，告诉机械手可以放料   --- 同步动作1
            _currentLayerStatusRobot = true;
            ResetPrecisionRobotSide(_currentLayerStatusRobot);
            if (!CheckFlowCondition()) return;
            SetSignal("机械手可放料");
            //等待放料并扫码
            Task t2 = Task.Run(() =>
            {
                WaitForSignal($"Robot{_moduleIndex + 1}模组", "放料完成");
                if (!CheckFlowCondition()) return;
                DoPrecisionRobotSide(_currentLayerStatusRobot, true, isDelay: false);
                if (!CheckFlowCondition()) return;
                //吹气打开、
                if (!GlobalVariable.ChangeScanPosition)
                {
                    if (SystemConfig.Instance.UsingStatus.IsScannerInUse)
                        _upRevacuumCylinder.On();

                    if (_feedTray1Up.Helper.Any(c => c.Status == CellStatus.HasProduct) &&
                        _feedTray2Up.Helper.Any(c => c.Status == CellStatus.HasProduct))
                    {

                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"当前上层精定位feed有料，开始扫码", this.Description, 0, ""));
                        //扫码上下气缸要顶起来
                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"顶起扫码上下气缸", this.Description, 0, ""));
                        //SingleCylinderOperate(_scanUpdownCylinder, 5000, true);

                        _scanUpdownCylinder.On();
                        Thread.Sleep(1000);
                        if (!CheckFlowCondition()) return;
                        //扫码
                        Task s1 = Task.Run(() =>
                        {
                            //先扫码的精定位靠近轨道那个
                            if (_feedTray1Up.Helper.GetCell(0).Status == CellStatus.HasProduct)
                            {
                                //if(_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray1Up.Helper.SetCellBarcode(0, ScanBarcode(BarcodeScanner1, 0));

                                //_feedTray1Up.Helper.SetCellBarcode(0, GlobalVariable.Robot2Up1Barcode);
                                //else if(_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray1Up.Helper.SetCellBarcode(0, ScanBarcode(_barcodeScanner1, 0));
                            }
                        });

                        Task s2 = Task.Run(() =>
                        {
                            if (_feedTray2Up.Helper.GetCell(0).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray2Up.Helper.SetCellBarcode(0, ScanBarcode(BarcodeScanner2, 1));
                                //_feedTray2Up.Helper.SetCellBarcode(0, GlobalVariable.Robot2Up5Barcode);
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray2Up.Helper.SetCellBarcode(0, ScanBarcode(_barcodeScanner2, 1));
                            }
                        });
                        Task.WaitAll(s1, s2);
                        if (!CheckFlowCondition()) return;
                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"扫码横移气缸推出", this.Description, 0, ""));
                        SingleCylinderOperate(_scanCylinder, 5000, true);
                        if (!CheckFlowCondition()) return;
                        //扫码
                        Task s3 = Task.Run(() =>
                        {
                            if (_feedTray1Up.Helper.GetCell(1).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray1Up.Helper.SetCellBarcode(1, ScanBarcode(BarcodeScanner1, 2));
                                //_feedTray1Up.Helper.SetCellBarcode(1, GlobalVariable.Robot2Up2Barcode);
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray1Up.Helper.SetCellBarcode(1, ScanBarcode(_barcodeScanner1, 2));
                            }
                        });
                        Task s4 = Task.Run(() =>
                        {
                            if (_feedTray2Up.Helper.GetCell(1).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray2Up.Helper.SetCellBarcode(1, ScanBarcode(BarcodeScanner2, 3));
                                //_feedTray2Up.Helper.SetCellBarcode(1, GlobalVariable.Robot2Up6Barcode);
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray2Up.Helper.SetCellBarcode(1, ScanBarcode(_barcodeScanner2, 3));
                            }
                        });
                        Task.WaitAll(s3, s4);
                        if (!CheckFlowCondition()) return;
                        //扫码气缸一定要下去
                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"扫码上下气缸下去", this.Description, 0, ""));
                        _scanUpdownCylinder.Off();
                        Thread.Sleep(1000);
                        MultiCylinderOperate(new List<Cylinder>() { _scanCylinder }, 5000, new List<bool>() { false });
                        //SingleCylinderOperate(_scanUpdownCylinder, 5000, false);
                        if (!CheckFlowCondition()) return;
                        //Task.Run(() =>
                        //{
                        //    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"扫码横移气缸回去", this.Description, 0, ""));
                        //    SingleCylinderOperate(_scanCylinder, 5000, false);
                        //});
                        if (!CheckFlowCondition()) return;

                    }
                    else if (_feedTray1Up.Helper.Any(c => c.Status == CellStatus.HasProduct))
                    {

                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"当前上层精定位只有feed1有料，开始扫码", this.Description, 0, ""));
                        //扫码上下气缸要顶起来
                        _scanUpdownCylinder.On();
                        Thread.Sleep(1000);

                        //SingleCylinderOperate(_scanUpdownCylinder, 5000, true);
                        if (!CheckFlowCondition()) return;
                        //扫码
                        Task s1 = Task.Run(() =>
                        {
                            if (_feedTray1Up.Helper.GetCell(0).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray1Up.Helper.SetCellBarcode(0, ScanBarcode(BarcodeScanner1, 0));
                                //_feedTray1Up.Helper.SetCellBarcode(0, GlobalVariable.Robot2Down1Barcode);
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray1Up.Helper.SetCellBarcode(0, ScanBarcode(_barcodeScanner1, 0));
                            }
                        });
                        Task.WaitAll(s1);
                        if (!CheckFlowCondition()) return;

                        SingleCylinderOperate(_scanCylinder, 5000, true);

                        if (!CheckFlowCondition()) return;

                        //扫码
                        Task s3 = Task.Run(() =>
                        {
                            if (_feedTray1Up.Helper.GetCell(1).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray1Up.Helper.SetCellBarcode(1, ScanBarcode(BarcodeScanner1, 1));
                                //_feedTray1Up.Helper.SetCellBarcode(1, GlobalVariable.Robot2Down2Barcode);
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray1Up.Helper.SetCellBarcode(1, ScanBarcode(_barcodeScanner1, 1));
                            }
                        });
                        Task.WaitAll(s3);
                        if (!CheckFlowCondition()) return;
                        //扫码气缸一定要下去
                        //扫码气缸一定要下去

                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"扫码上下气缸下去", this.Description, 0, ""));
                        _scanUpdownCylinder.Off();
                        Thread.Sleep(1000);
                        MultiCylinderOperate(new List<Cylinder>() { _scanCylinder }, 5000, new List<bool>() { false });
                        //SingleCylinderOperate(_scanUpdownCylinder, 5000, false);
                        if (!CheckFlowCondition()) return;

                    }
                    else if (_feedTray2Up.Helper.Any(c => c.Status == CellStatus.HasProduct))
                    {

                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"当前上层精定位只有feed2有料，开始扫码", this.Description, 0, ""));
                        //扫码上下气缸要顶起来
                        //SingleCylinderOperate(_scanUpdownCylinder, 5000, true);
                        _scanUpdownCylinder.On();
                        Thread.Sleep(1000);

                        if (!CheckFlowCondition()) return;
                        //扫码
                        Task s2 = Task.Run(() =>
                        {
                            if (_feedTray2Up.Helper.GetCell(0).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray2Up.Helper.SetCellBarcode(0, ScanBarcode(BarcodeScanner2, 2));
                                //_feedTray2Up.Helper.SetCellBarcode(0, GlobalVariable.Robot2Down5Barcode);
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray2Up.Helper.SetCellBarcode(0, ScanBarcode(_barcodeScanner2, 2));
                            }
                        });
                        Task.WaitAll(s2);
                        if (!CheckFlowCondition()) return;
                        SingleCylinderOperate(_scanCylinder, 5000, true);
                        if (!CheckFlowCondition()) return;

                        //扫码
                        Task s4 = Task.Run(() =>
                        {
                            if (_feedTray2Up.Helper.GetCell(1).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray2Up.Helper.SetCellBarcode(1, ScanBarcode(BarcodeScanner2, 3));
                                //_feedTray2Up.Helper.SetCellBarcode(1, GlobalVariable.Robot2Down6Barcode);
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray2Up.Helper.SetCellBarcode(1, ScanBarcode(_barcodeScanner2, 3));
                            }
                        });
                        Task.WaitAll(s4);
                        if (!CheckFlowCondition()) return;

                        //扫码气缸一定要下去
                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"扫码上下气缸下去", this.Description, 0, ""));

                        _scanUpdownCylinder.Off();
                        Thread.Sleep(1000);
                        MultiCylinderOperate(new List<Cylinder>() { _scanCylinder }, 5000, new List<bool>() { false });

                        //SingleCylinderOperate(_scanUpdownCylinder, 5000, false);
                        if (!CheckFlowCondition()) return;

                    }
                }
                //吹气关闭
                _upRevacuumCylinder.Off();
            });
            //告诉arm下层可取
            _currentLayerStatus = false;
            if (_moduleIndex == 0)
            {
                ResetPrecisionArmSide(_currentLayerStatus, isDelay: false);
                if (!CheckFlowCondition()) return;
                SetSignal("Arm1精定位到位");
                SetSignal("Arm2精定位到位");
                WaitForSignal("Arm1模组", "Arm1取精定位完成");
                if (!CheckFlowCondition()) return;
                WaitForSignal("Arm2模组", "Arm2取精定位完成");
                if (!CheckFlowCondition()) return;
                DoPrecisionArmSide(_currentLayerStatus, true, isDelay: false);
                if (!CheckFlowCondition()) return;
            }
            else
            {
                ResetPrecisionArmSide(_currentLayerStatus, isDelay: false);
                if (!CheckFlowCondition()) return;
                SetSignal("Arm3精定位到位");
                SetSignal("Arm4精定位到位");
                WaitForSignal("Arm3模组", "Arm3取精定位完成");
                if (!CheckFlowCondition()) return;
                WaitForSignal("Arm4模组", "Arm4取精定位完成");
                if (!CheckFlowCondition()) return;
                DoPrecisionArmSide(_currentLayerStatus, true, isDelay: false);
                if (!CheckFlowCondition()) return;
            }

            Task.WaitAll(t2);
            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"机械手放料上层OK", this.Description, 0, _scanNG1InPosition.Module));

            if (!CheckFlowCondition()) return;
            //检测上层是否一颗料都没有，没有的话，不需要把下层移过去
            if ((!_feedTray1Up.Helper.Any(c => c.Status == CellStatus.HasProduct)) &&
               (!_feedTray2Up.Helper.Any(c => c.Status == CellStatus.HasProduct)) &&
               (!_recycleTray1Down.Helper.Any(c => c.Status == CellStatus.HasProduct)) &&
               (!_recycleTray2Down.Helper.Any(c => c.Status == CellStatus.HasProduct)))
            {
                MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"上层一颗料都没有，不需要把下层移过去", this.Description, 0, _scanNG1InPosition.Module));
                //结束前，判断下，是否是清料
                if (_moduleIndex == 0)
                {
                    if (GlobalVariable.IsClearLater || GlobalVariable.IsClearNow)
                    {
                        if (CommonMethod.AllTesterNoProduct(_moduleIndex == 0 ? 0 : 2) &&
                            CommonMethod.AllArmNoProduct(_moduleIndex) &&
                            CommonMethod.AllPrecisionNoProduct(_moduleIndex) &&
                            CommonMethod.AllRobotNoProdcut(_moduleIndex) &&
                            ((!CommonMethod.ANGHasProduct(_moduleIndex) &&
                              !CommonMethod.ABNGHasProduct(_moduleIndex)) ||
                              !GlobalVariable.IsOnlineRetest))
                        {
                            if (CommonMethod.BufferHasProduct(_moduleIndex))
                            {
                                SetSignal("清空Buffer");
                                if (_moduleIndex == 0)
                                    WaitForSignal("前模组清料", "清空Buffer完成");
                                else
                                    WaitForSignal("后模组清料", "清空Buffer完成");
                                if (!CheckFlowCondition()) return;
                                goto downstart;
                            }
                            GlobalVariable.IsClearDownTray = true;
                            if (GlobalVariable.IsClearDownTray)
                            {
                                while (!GlobalVariable.IsClearDownTrayFinish && !IsBreak())
                                {
                                    Thread.Sleep(20);
                                }
                                if (!CheckFlowCondition()) return;
                                GlobalVariable.IsClearDownTrayFinish = false;
                            }

                            while (!GlobalVariable.BackClearFinish && !IsBreak() && (!SystemConfig.Instance.UsingStatus.IsRobot2Disable && GlobalVariable.PutOKNGBack.Status.IsEnabled))
                            {
                                Thread.Sleep(20);
                            }
                            GlobalVariable.BackClearFinish = false;
                            GlobalVariable.FrontClearFinish = true;
                            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, "清料完成", this.Description, 0, "系统"));
                            ShowWarnAndPause("清料完成!", "系统", true);
                            //GlobalVariable.IsClearFinish = true;
                            if (!CheckFlowCondition()) return;
                            _flowManager.ResetAllSignal();
                            GlobalVariable.IsClearLater = false;
                            GlobalVariable.IsClearNow = false;
                            GlobalVariable.IsClearLater2 = false;
                            GlobalVariable.IsClearNow2 = false;
                            GlobalVariable.IsClearDownTray = false;
                            GlobalVariable.IsClearDownTray2 = false;
                        }
                    }
                }
                else
                {
                    if (GlobalVariable.IsClearLater2 || GlobalVariable.IsClearNow2)
                    {
                        if (CommonMethod.AllTesterNoProduct(_moduleIndex == 0 ? 0 : 2) &&
                            CommonMethod.AllArmNoProduct(_moduleIndex) &&
                            CommonMethod.AllPrecisionNoProduct(_moduleIndex) &&
                            CommonMethod.AllRobotNoProdcut(_moduleIndex) &&
                            ((!CommonMethod.ANGHasProduct(_moduleIndex) &&
                              !CommonMethod.ABNGHasProduct(_moduleIndex)) ||
                              !GlobalVariable.IsOnlineRetest))
                        {
                            if (CommonMethod.BufferHasProduct(_moduleIndex))
                            {
                                SetSignal("清空Buffer");
                                if (_moduleIndex == 0)
                                    WaitForSignal("前模组清料", "清空Buffer完成");
                                else
                                    WaitForSignal("后模组清料", "清空Buffer完成");
                                if (!CheckFlowCondition()) return;
                                goto downstart;
                            }

                            GlobalVariable.IsClearDownTray2 = true;
                            if (GlobalVariable.IsClearDownTray2)
                            {
                                while (!GlobalVariable.IsClearDownTrayFinish2 && !IsBreak())
                                {
                                    Thread.Sleep(20);
                                }
                                if (!CheckFlowCondition()) return;
                                GlobalVariable.IsClearDownTrayFinish2 = false;
                            }

                            GlobalVariable.BackClearFinish = true;
                            while ((!GlobalVariable.FrontClearFinish && !SystemConfig.Instance.UsingStatus.IsFrontDisable) && !IsBreak() && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                            {
                                Thread.Sleep(20);
                            }
                            GlobalVariable.FrontClearFinish = false;
                            Thread.Sleep(1000);
                            if (!CheckFlowCondition()) return;
                            while (GlobalVariable.IsClearDownTray2 && !IsBreak())
                            {
                                Thread.Sleep(20);
                            }

                            if (SystemConfig.Instance.UsingStatus.IsFrontDisable)
                            {
                                GlobalVariable.BackClearFinish = false;
                                MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, "清料完成", this.Description, 0, "系统"));
                                ShowWarnAndPause("清料完成!", "系统", true);
                                //GlobalVariable.IsClearFinish = true;
                                if (!CheckFlowCondition()) return;
                                _flowManager.ResetAllSignal();
                                GlobalVariable.IsClearLater = false;
                                GlobalVariable.IsClearNow = false;
                                GlobalVariable.IsClearLater2 = false;
                                GlobalVariable.IsClearNow2 = false;
                                GlobalVariable.IsClearDownTray = false;
                                GlobalVariable.IsClearDownTray2 = false;
                            }
                        }
                    }
                }
                goto upstart;
            }
        //如果下层 arm侧 有待测料还没取走  那也不需要把下层移过去
        //if(_feedTray1Down.Helper.Any(c => c.Status == CellStatus.HasProduct) ||
        //   _feedTray2Down.Helper.Any(c => c.Status == CellStatus.HasProduct))
        //{
        //    goto upstart;
        //}

        downstart:
            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"交换上下层,下层至机械手侧", this.Description, 0, _scanNG1InPosition.Module));
            SwitchOnce(isDownInRobotFeed: true, _moduleIndex);
            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"交换上下层完成", this.Description, 0, _scanNG1InPosition.Module));
            if (!CheckFlowCondition()) return;
            _currentLayerStatusRobot = false;
            ResetPrecisionRobotSide(_currentLayerStatusRobot);
            SetSignal("机械手可放料");
            //等待放料并扫码
            Task t1 = Task.Run(() =>
            {
                WaitForSignal($"Robot{_moduleIndex + 1}模组", "放料完成");
                if (!CheckFlowCondition()) return;
                DoPrecisionRobotSide(_currentLayerStatusRobot, true, isDelay: false);
                //吹气打开
                if (!GlobalVariable.ChangeScanPosition)
                {
                    if (SystemConfig.Instance.UsingStatus.IsScannerInUse)
                        _downRevacuumCylinder.On();
                    if (_feedTray1Down.Helper.Any(c => c.Status == CellStatus.HasProduct) &&
                        _feedTray2Down.Helper.Any(c => c.Status == CellStatus.HasProduct))
                    {

                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"当前下层精定位feed有料，开始扫码", this.Description, 0, ""));
                        //扫码
                        Task s1 = Task.Run(() =>
                        {
                            if (_feedTray1Down.Helper.GetCell(0).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray1Down.Helper.SetCellBarcode(0, ScanBarcode(BarcodeScanner1, 0));
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray1Down.Helper.SetCellBarcode(0, ScanBarcode(_barcodeScanner1, 0));
                            }
                        });
                        Task s2 = Task.Run(() =>
                        {
                            if (_feedTray2Down.Helper.GetCell(0).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray2Down.Helper.SetCellBarcode(0, ScanBarcode(BarcodeScanner2, 1));
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray2Down.Helper.SetCellBarcode(0, ScanBarcode(_barcodeScanner2, 1));
                            }
                        });
                        Task.WaitAll(s1, s2);

                        SingleCylinderOperate(_scanCylinder, 5000, true);
                        if (!CheckFlowCondition()) return;

                        //扫码
                        Task s3 = Task.Run(() =>
                        {
                            if (_feedTray1Down.Helper.GetCell(1).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray1Down.Helper.SetCellBarcode(1, ScanBarcode(BarcodeScanner1, 2));
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray1Down.Helper.SetCellBarcode(1, ScanBarcode(_barcodeScanner1, 2));
                            }
                        });
                        Task s4 = Task.Run(() =>
                        {
                            if (_feedTray2Down.Helper.GetCell(1).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray2Down.Helper.SetCellBarcode(1, ScanBarcode(BarcodeScanner2, 3));
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray2Down.Helper.SetCellBarcode(1, ScanBarcode(_barcodeScanner2, 3));
                            }
                        });
                        Task.WaitAll(s3, s4);
                        SingleCylinderOperate(_scanCylinder, 5000, false);
                        if (!CheckFlowCondition()) return;

                    }
                    else if (_feedTray1Down.Helper.Any(c => c.Status == CellStatus.HasProduct))
                    {

                        //扫码
                        Task s1 = Task.Run(() =>
                        {
                            if (_feedTray1Down.Helper.GetCell(0).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray1Down.Helper.SetCellBarcode(0, ScanBarcode(BarcodeScanner1, 4));
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray1Down.Helper.SetCellBarcode(0, ScanBarcode(_barcodeScanner1, 4));
                            }
                        });
                        Task.WaitAll(s1);

                        SingleCylinderOperate(_scanCylinder, 5000, true);
                        if (!CheckFlowCondition()) return;

                        //扫码
                        Task s3 = Task.Run(() =>
                        {
                            if (_feedTray1Down.Helper.GetCell(1).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray1Down.Helper.SetCellBarcode(1, ScanBarcode(BarcodeScanner1, 5));
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray1Down.Helper.SetCellBarcode(1, ScanBarcode(_barcodeScanner1, 5));
                            }
                        });
                        Task.WaitAll(s3);
                        SingleCylinderOperate(_scanCylinder, 5000, false);
                        if (!CheckFlowCondition()) return;

                    }
                    else if (_feedTray2Down.Helper.Any(c => c.Status == CellStatus.HasProduct))
                    {

                        //扫码
                        Task s2 = Task.Run(() =>
                        {
                            if (_feedTray2Down.Helper.GetCell(0).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray2Down.Helper.SetCellBarcode(0, ScanBarcode(BarcodeScanner2, 6));
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray2Down.Helper.SetCellBarcode(0, ScanBarcode(_barcodeScanner2, 6));
                            }
                        });
                        Task.WaitAll(s2);

                        SingleCylinderOperate(_scanCylinder, 5000, true);
                        if (!CheckFlowCondition()) return;

                        //扫码
                        Task s4 = Task.Run(() =>
                        {
                            if (_feedTray2Down.Helper.GetCell(1).Status == CellStatus.HasProduct)
                            {
                                //if (_moduleIndex == 0 && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                                _feedTray2Down.Helper.SetCellBarcode(1, ScanBarcode(BarcodeScanner2, 7));
                                //else if (_moduleIndex == 1 && !SystemConfig.Instance.UsingStatus.IsRobot2Disable)
                                //    _feedTray2Down.Helper.SetCellBarcode(1, ScanBarcode(_barcodeScanner2, 7));
                            }
                        });
                        Task.WaitAll(s4);
                        SingleCylinderOperate(_scanCylinder, 5000, false);
                        if (!CheckFlowCondition()) return;

                    }
                    //吹气关闭
                    _downRevacuumCylinder.Off();
                }
            });
            //告诉arm下层可取
            _currentLayerStatus = true;
            if (_moduleIndex == 0)
            {
                ResetPrecisionArmSide(_currentLayerStatus, isDelay: false);
                if (!CheckFlowCondition()) return;
                SetSignal("Arm1精定位到位");
                SetSignal("Arm2精定位到位");
                WaitForSignal("Arm1模组", "Arm1取精定位完成");
                if (!CheckFlowCondition()) return;
                WaitForSignal("Arm2模组", "Arm2取精定位完成");
                if (!CheckFlowCondition()) return;
                DoPrecisionArmSide(_currentLayerStatus, true, isDelay: false);
                if (!CheckFlowCondition()) return;
            }
            else
            {
                ResetPrecisionArmSide(_currentLayerStatus, isDelay: false);
                if (!CheckFlowCondition()) return;
                SetSignal("Arm3精定位到位");
                SetSignal("Arm4精定位到位");
                WaitForSignal("Arm3模组", "Arm3取精定位完成");
                if (!CheckFlowCondition()) return;
                WaitForSignal("Arm4模组", "Arm4取精定位完成");
                if (!CheckFlowCondition()) return;
                DoPrecisionArmSide(_currentLayerStatus, true, isDelay: false);
                if (!CheckFlowCondition()) return;
            }

            Task.WaitAll(t1);
            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"机械手放料下层OK", this.Description, 0, _scanNG1InPosition.Module));
            if (!CheckFlowCondition()) return;
            //检测上层是否一颗料都没有，没有的话，不需要把下层移过去
            if ((!_feedTray1Down.Helper.Any(c => c.Status == CellStatus.HasProduct)) &&
                (!_feedTray2Down.Helper.Any(c => c.Status == CellStatus.HasProduct)) &&
                (!_recycleTray1Up.Helper.Any(c => c.Status == CellStatus.HasProduct)) &&
                (!_recycleTray2Up.Helper.Any(c => c.Status == CellStatus.HasProduct)))
            {
                MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"下层一颗料都没有，不需要把下层移过去", this.Description, 0, _scanNG1InPosition.Module));
                //结束前，判断下，是否是清料
                if (_moduleIndex == 0)
                {
                    if (GlobalVariable.IsClearLater || GlobalVariable.IsClearNow)
                    {
                        if (CommonMethod.AllTesterNoProduct(_moduleIndex == 0 ? 0 : 2) &&
                            CommonMethod.AllArmNoProduct(_moduleIndex) &&
                            CommonMethod.AllPrecisionNoProduct(_moduleIndex) &&
                            CommonMethod.AllRobotNoProdcut(_moduleIndex) &&
                            ((!CommonMethod.ANGHasProduct(_moduleIndex) &&
                              !CommonMethod.ABNGHasProduct(_moduleIndex)) ||
                              !GlobalVariable.IsOnlineRetest))
                        {
                            if (CommonMethod.BufferHasProduct(_moduleIndex))
                            {
                                SetSignal("清空Buffer");
                                if (_moduleIndex == 0)
                                    WaitForSignal("前模组清料", "清空Buffer完成");
                                else
                                    WaitForSignal("后模组清料", "清空Buffer完成");
                                if (!CheckFlowCondition()) return;
                                goto upstart;
                            }
                            GlobalVariable.IsClearDownTray = true;
                            if (GlobalVariable.IsClearDownTray)
                            {
                                while (!GlobalVariable.IsClearDownTrayFinish && !IsBreak())
                                {
                                    Thread.Sleep(20);
                                }
                                if (!CheckFlowCondition()) return;
                                GlobalVariable.IsClearDownTrayFinish = false;
                            }
                            while (!GlobalVariable.BackClearFinish && !IsBreak() && !SystemConfig.Instance.UsingStatus.IsRobot2Disable && GlobalVariable.PutOKNGBack.Status.IsEnabled)
                            {
                                Thread.Sleep(20);
                            }
                            GlobalVariable.BackClearFinish = false;
                            GlobalVariable.FrontClearFinish = true;
                            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, "清料完成", this.Description, 0, "系统"));
                            ShowWarnAndPause("清料完成!", "系统", true);
                            //GlobalVariable.IsClearFinish = true;
                            if (!CheckFlowCondition()) return;
                            _flowManager.ResetAllSignal();
                            GlobalVariable.IsClearLater = false;
                            GlobalVariable.IsClearNow = false;
                            GlobalVariable.IsClearLater2 = false;
                            GlobalVariable.IsClearNow2 = false;
                            GlobalVariable.IsClearDownTray = false;
                        }
                    }
                }
                else
                {
                    if (GlobalVariable.IsClearLater2 || GlobalVariable.IsClearNow2)
                    {
                        if (CommonMethod.AllTesterNoProduct(_moduleIndex == 0 ? 0 : 2) &&
                            CommonMethod.AllArmNoProduct(_moduleIndex) &&
                            CommonMethod.AllPrecisionNoProduct(_moduleIndex) &&
                            CommonMethod.AllRobotNoProdcut(_moduleIndex) &&
                            ((!CommonMethod.ANGHasProduct(_moduleIndex) &&
                              !CommonMethod.ABNGHasProduct(_moduleIndex)) ||
                              !GlobalVariable.IsOnlineRetest))
                        {
                            if (CommonMethod.BufferHasProduct(_moduleIndex))
                            {
                                SetSignal("清空Buffer");
                                if (_moduleIndex == 0)
                                    WaitForSignal("前模组清料", "清空Buffer完成");
                                else
                                    WaitForSignal("后模组清料", "清空Buffer完成");
                                if (!CheckFlowCondition()) return;
                                goto upstart;
                            }

                            GlobalVariable.IsClearDownTray2 = true;
                            if (GlobalVariable.IsClearDownTray2)
                            {
                                while (!GlobalVariable.IsClearDownTrayFinish2 && !IsBreak())
                                {
                                    Thread.Sleep(20);
                                }
                                if (!CheckFlowCondition()) return;
                                GlobalVariable.IsClearDownTrayFinish2 = false;
                            }

                            GlobalVariable.BackClearFinish = true;
                            while (!GlobalVariable.FrontClearFinish && !IsBreak() && !SystemConfig.Instance.UsingStatus.IsRobot1Disable)
                            {
                                Thread.Sleep(20);
                            }
                            GlobalVariable.FrontClearFinish = false;
                            Thread.Sleep(1000);
                            if (!CheckFlowCondition()) return;
                            while (GlobalVariable.IsClearDownTray2 && !IsBreak())
                            {
                                Thread.Sleep(20);
                            }

                            if (SystemConfig.Instance.UsingStatus.IsFrontDisable)
                            {
                                GlobalVariable.BackClearFinish = false;
                                MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, "清料完成", this.Description, 0, "系统"));
                                ShowWarnAndPause("清料完成!", "系统", true);
                                //GlobalVariable.IsClearFinish = true;
                                if (!CheckFlowCondition()) return;
                                _flowManager.ResetAllSignal();
                                GlobalVariable.IsClearLater = false;
                                GlobalVariable.IsClearNow = false;
                                GlobalVariable.IsClearLater2 = false;
                                GlobalVariable.IsClearNow2 = false;
                                GlobalVariable.IsClearDownTray = false;
                                GlobalVariable.IsClearDownTray2 = false;
                            }
                        }
                    }
                }
                goto downstart;
            }
            //如果上层 arm侧 有待测料还没取走  那也不需要把下层移过去
            //if (_feedTray1Up.Helper.Any(c => c.Status == CellStatus.HasProduct) ||
            //    _feedTray2Up.Helper.Any(c => c.Status == CellStatus.HasProduct))
            //{
            //    goto downstart;
            //}
        }
        public bool MesCheckIn(string sn)
        {
            if (SystemConfig.Instance.MesParam.EnableMES)
            {
            retry:
                if (!CommonMethod.DoMesCheckIn(sn, out string msg))
                {
                    if (SystemConfig.Instance.UsingStatus.IsMesCheckInWarnEnable)
                    {
                        ShowWarnAndPause($"MES CheckIn失败!原因:{msg}", "系统");
                        if (!CheckFlowCondition()) return false;
                        return false;
                        //if (MessageBox.Show("是否重试?", "重试CHECKIN", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        //{
                        //    goto retry;
                        //}
                        //else
                        //{
                        //    return false;
                        //}
                    }
                    else
                    {
                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"MES CheckIn失败!原因:{msg}", this.Description, 0, ""));
                        return false;
                    }
                }
                if (msg.ToLower().Contains("config:"))
                {
                    GlobalVariable.MesConfig = msg;
                }
            }
            else
                GlobalVariable.MesConfig = "";
            return true;
        }
        public override void OnStop()
        {
            base.OnStop();
        }

        public override bool PreStartCheck()
        {
            return base.PreStartCheck();
        }
        int scanCounttttt = 0;
        int scanCount2 = 0;
        public string ScanBarcode(CommunicationHelper scanner, int scanIndex)
        {
        rescan:
            scanCounttttt++;
            if (scanCounttttt == 1)
            {
                MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"开始扫码", this.Description, 0, ""));
            }
            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"开始第{scanCounttttt}次扫码", this.Description, 0, ""));
            if (!GlobalVariable.IsDryRun && SystemConfig.Instance.UsingStatus.IsScannerInUse)
            {
                SendPackage sp = new SendPackage();
                sp.Message = "LON\r\n";
                TcpClientSendAndReceive(scanner,
                                        sp,
                                        2000,
                                        3,
                                        out string rawString);
                string barcode = "";
                if (string.IsNullOrEmpty(rawString) || rawString.Contains("error"))
                {
                    if (SystemConfig.Instance.UsingStatus.IsScanPassEnable)
                    {
                        scanCounttttt = 0;
                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"{scanner.Description}扫码强制Pass！", this.Description, 0, ""));
                        barcode = $"{DateTime.Now.ToString("yyyyMMddHHmmssfff")}";
                    }
                    else
                    {
                        barcode = "NG";
                        if (scanCounttttt < 3)
                        {
                            goto rescan;
                        }
                        else
                        {
                            scanCounttttt = 0;
                            if (SystemConfig.Instance.UsingStatus.IsScannerWarnEnable)
                            {
                                ShowWarnAndPause($"{scanner.Description}连续三次扫码失败！", this.Description);
                                if (MessageBox.Show("是否重试?", "扫码", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                {
                                    goto rescan;
                                }
                            }
                            else
                                MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"{scanner.Description}连续三次扫码失败！", this.Description, 0, ""));
                        }
                    }
                }
                else
                {
                    scanCounttttt = 0;
                    barcode = rawString.Replace("\r", "").Replace("\n", "").Trim();
                }
                if (barcode != "NG")
                {
                    if (!MesCheckIn(barcode))
                    {
                        barcode = "NG";
                        lock (GlobalVariable.FrontBackMesDataLock)
                        {
                            GlobalVariable.CurrentMachineData.TotalMesNGCount += 1;
                        }
                        MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalMesNGCount.ToString(), this.Description, 0, "MesNG"));
                    }
                }
                else
                {
                    lock (GlobalVariable.FrontBackScanDataLock)
                    {
                        GlobalVariable.CurrentMachineData.TotalScanNGCount += 1;
                    }
                    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Count, GlobalVariable.CurrentMachineData.TotalScanNGCount.ToString(), this.Description, 0, "ScanNG"));
                }
                return barcode;
            }
            else
            {
                Thread.Sleep(500);
                //Random r = new Random();
                //int i = r.Next(1, 50);
                //if (i == 1)
                //    return "NG";
                if (GlobalVariable.IsGrrMode)
                {
                    lock (scanLockObj)
                    {
                        scanCount2++;
                    }
                    if (SystemConfig.Instance.SpotSNs.SNGrr == null || SystemConfig.Instance.SpotSNs.SNGrr.Length == 0)
                        return $"VSN{DateTime.Now.ToString("yyyyMMddHHmmssfff")}-{scanIndex}";
                    if (scanCount2 == 1)
                        return SystemConfig.Instance.SpotSNs.SNGrr[scanCount2 - 1];
                    if (scanCount2 == 2)
                        return SystemConfig.Instance.SpotSNs.SNGrr[scanCount2 - 1];
                    if (scanCount2 == 3)
                        return SystemConfig.Instance.SpotSNs.SNGrr[scanCount2 - 1];
                    if (scanCount2 == 4)
                        return SystemConfig.Instance.SpotSNs.SNGrr[scanCount2 - 1];
                    if (scanCount2 == 5)
                        return SystemConfig.Instance.SpotSNs.SNGrr[scanCount2 - 1];
                    if (scanCount2 == 6)
                        return SystemConfig.Instance.SpotSNs.SNGrr[scanCount2 - 1];
                }
                return $"VSN{DateTime.Now.ToString("yyyyMMddHHmmssfff")}-{scanIndex}";
            }
        }
        //上层推出，下层缩回 - true
        //上层缩回，下层推出 - false
        public bool GetTwoLayerInPositionStatus()
        {
            return _currentLayerStatus;
        }
        public bool GetTwoLayerInPositionStatusRobot()
        {
            return _currentLayerStatusRobot;
        }
        public Tray GetFeedTray1Up()
        {
            return _feedTray1Up;
        }
        public Tray GetFeedTray2Up()
        {
            return _feedTray2Up;
        }
        public Tray GetRecycleTray1Up()
        {
            return _recycleTray1Up;
        }
        public Tray GetRecycleTray2Up()
        {
            return _recycleTray2Up;
        }
        public Tray GetFeedTray1Down()
        {
            return _feedTray1Down;
        }
        public Tray GetFeedTray2Down()
        {
            return _feedTray2Down;
        }
        public Tray GetRecycleTray1Down()
        {
            return _recycleTray1Down;
        }
        public Tray GetRecycleTray2Down()
        {
            return _recycleTray2Down;
        }
        public void SwitchOnce(bool isDownInRobotFeed, int moduleIndex)
        {
            CTMoveStartCalculateNode();
            InSingleRunMode();
            while (GlobalVariable.RobotZInNotSafeArea[moduleIndex] && !IsBreak())
            {
                ShowWarnAndPause($" 机器人{moduleIndex + 1} Z轴不在安全位，无法移动精定位移载气缸", this.Description);
                if (!CheckFlowCondition()) return;
                Thread.Sleep(2);
            }
            if (!CheckFlowCondition()) return;
            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Log, $"精定位上下层切换", this.Description, 0, _separateCylinderUp.Module));

            if (!isDownInRobotFeed)
            {
                _upMoveCylinder.On();
                _separateCylinderUp.On();
                _downMoveCylinder.Off();
                Thread.Sleep(SystemConfig.Instance.UsingStatus.CylinderSeparateOutDelay);
                _separateCylinderDown.Off();
            }
            else
            {
                _separateCylinderDown.On();
                _downMoveCylinder.On();
                _upMoveCylinder.Off();
                Thread.Sleep(SystemConfig.Instance.UsingStatus.CylinderSeparateOutDelay);
                _separateCylinderUp.Off();
            }
            MultiCylinderWait(new List<Cylinder>()
            {
                _upMoveCylinder,
                _separateCylinderUp,
                _downMoveCylinder,
                _separateCylinderDown
            }, 5000, new List<bool>()
            {
                !isDownInRobotFeed, !isDownInRobotFeed, isDownInRobotFeed, isDownInRobotFeed,
            });

            if (isDownInRobotFeed)
                UpHeaderPos = HeaderPosition.ArmSide;
            else
                UpHeaderPos = HeaderPosition.RobotSide;
            CTMoveEndCalculateNode(out double time);
            MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.CT, time.ToString("0.0"), "", 0, $"精定位{_moduleIndex + 1}交互"));
            InSingleRunMode();
        }
        public void ResetPrecisionRobotSide(bool isUp)
        {
            InSingleRunMode();
            //if (_moduleIndex == 0)
            //{
            //    if (isUp)
            //    {
            //        _upRecycleVacs.ForEach(v => v.On());
            //        MultiCylinderOperate(new List<Cylinder>() { _upPositionCylinder1, _upPositionCylinder2 },
            //        5000,
            //                             new List<bool>() { false, false });
            //    }
            //    else
            //    {
            //        _downRecycleVacs.ForEach(v => v.On());
            //        MultiCylinderOperate(new List<Cylinder>() { _downPositionCylinder1, _downPositionCylinder2 },
            //        5000,
            //                             new List<bool>() { false, false });
            //    }
            //}
            //else
            //{
            if (isUp)
            {
                //_upRecycleVacs.ForEach(v => v.On());
                MultiCylinderOperate(_upRecyclePositionCylinder,
                   5000,
                   new List<bool>() { false, false });
                //List<Cylinder> _upPositionCylinder = new List<Cylinder>();
                //    _upPositionCylinder.AddRange(_upFeedPositionCylinder);
                //    _upPositionCylinder.AddRange(_upRecyclePositionCylinder);
                //    MultiCylinderOperate(_upPositionCylinder,
                //    5000,
                //                         new List<bool>() { false, false, false, false });
            }
            else
            {
                //_downRecycleVacs.ForEach(v => v.On());
                MultiCylinderOperate(_downRecyclePositionCylinder,
                  5000,
                  new List<bool>() { false, false });
                //List<Cylinder> _downPositionCylinder = new List<Cylinder>();
                //_downPositionCylinder.AddRange(_downFeedPositionCylinder);
                //_downPositionCylinder.AddRange(_downRecyclePositionCylinder);
                //MultiCylinderOperate(_downPositionCylinder,
                //  5000,
                //                       new List<bool>() { false, false, false, false });
            }
            //}
            InSingleRunMode();
        }
        public void ResetPrecisionArmSide(bool isUp, bool isDelay = true)
        {
            //if (_moduleIndex == 0)
            //{
            //    if (isUp)
            //    {
            //        if (isDelay)
            //            Thread.Sleep(SystemConfig.Instance.UsingStatus.ReleasePrecisionDelayBeforeInArmUp);
            //        _upFeedVacs.ForEach(v => v.On());
            //        MultiCylinderOperate(new List<Cylinder>() { _upPositionCylinder1, _upPositionCylinder2 },
            //        5000,
            //                             new List<bool>() { false, false });
            //        if (isDelay)
            //            Thread.Sleep(SystemConfig.Instance.UsingStatus.ReleasePrecisionDelayInArmUp);
            //    }
            //    else
            //    {
            //        if (isDelay)
            //            Thread.Sleep(SystemConfig.Instance.UsingStatus.ReleasePrecisionDelayBeforeInArmDown);
            //        _downFeedVacs.ForEach(v => v.On());
            //        MultiCylinderOperate(new List<Cylinder>() { _downPositionCylinder1, _downPositionCylinder2 },
            //        5000,
            //                             new List<bool>() { false, false });
            //        if (isDelay)
            //            Thread.Sleep(SystemConfig.Instance.UsingStatus.ReleasePrecisionDelayInArmDown);
            //    }

            //}
            //else
            //{
            //    //if (isUp)
            //    //{
            //    //    if (isDelay)
            //    //        Thread.Sleep(SystemConfig.Instance.UsingStatus.ReleasePrecisionDelayBeforeInArmUp);
            //    //    _upFeedVacs.ForEach(v => v.On());
            //    //    MultiCylinderOperate(_upFeedPositionCylinder,
            //    //    5000,
            //    //                         new List<bool>() { false, false });
            //    //    if (isDelay)
            //    //        Thread.Sleep(SystemConfig.Instance.UsingStatus.ReleasePrecisionDelayInArmUp);
            //    //}
            //    //else
            //    //{
            //    //    if (isDelay)
            //    //        Thread.Sleep(SystemConfig.Instance.UsingStatus.ReleasePrecisionDelayBeforeInArmDown);
            //    //    _downFeedVacs.ForEach(v => v.On());
            //    //    MultiCylinderOperate(_downFeedPositionCylinder,
            //    //    5000,
            //    //                         new List<bool>() { false, false });
            //    //    if (isDelay)
            //    //        Thread.Sleep(SystemConfig.Instance.UsingStatus.ReleasePrecisionDelayInArmDown);
            //    //}
            //}
        }
        public void DoPrecisionRobotSide(bool isUp, bool onoff, bool isDelay = true)
        {
            //if (_moduleIndex == 0)
            //{
            //    if (isUp)
            //    {
            //        if (isDelay)
            //            Thread.Sleep(SystemConfig.Instance.UsingStatus.ClampPrecisionDelayBeforeInArmUp);
            //        _upFeedVacs.ForEach(v => v.Off());
            //        MultiCylinderOperate(new List<Cylinder>() { _upPositionCylinder1, _upPositionCylinder2 },
            //                             5000,
            //                             new List<bool>() { onoff, onoff });
            //        if (isDelay)
            //            Thread.Sleep(SystemConfig.Instance.UsingStatus.ClampPrecisionDelayInArmUp);
            //    }
            //    else
            //    {
            //        if (isDelay)
            //            Thread.Sleep(SystemConfig.Instance.UsingStatus.ClampPrecisionDelayBeforeInArmDown);
            //        _downFeedVacs.ForEach(v => v.Off());
            //        MultiCylinderOperate(new List<Cylinder>() { _downPositionCylinder1, _downPositionCylinder2 },
            //                             5000,
            //                             new List<bool>() { onoff, onoff });
            //        if (isDelay)
            //            Thread.Sleep(SystemConfig.Instance.UsingStatus.ClampPrecisionDelayInArmDown);
            //    }
            //}
            //else
            //{
            InSingleRunMode();
            if (isUp)
            {
                if (isDelay)
                    Thread.Sleep(SystemConfig.Instance.UsingStatus.ClampPrecisionDelayBeforeInArmUp);
                //_upFeedVacs.ForEach(v => v.Off());
                MultiCylinderOperate(_upFeedPositionCylinder,
                                     5000,
                                     new List<bool>() { onoff, onoff });
                if (isDelay)
                    Thread.Sleep(SystemConfig.Instance.UsingStatus.ClampPrecisionDelayInArmUp);
            }
            else
            {
                if (isDelay)
                    Thread.Sleep(SystemConfig.Instance.UsingStatus.ClampPrecisionDelayBeforeInArmDown);
                //_downFeedVacs.ForEach(v => v.Off());
                MultiCylinderOperate(_downFeedPositionCylinder,
                                     5000,
                                     new List<bool>() { onoff, onoff });
                if (isDelay)
                    Thread.Sleep(SystemConfig.Instance.UsingStatus.ClampPrecisionDelayInArmDown);
            }
            InSingleRunMode();
            //}
        }
        public void DoPrecisionArmSide(bool isUp, bool onoff, bool isDelay)
        {
            //if (_moduleIndex == 0)
            //{
            //    if (isUp)
            //    {
            //        if (isDelay)
            //            Thread.Sleep(SystemConfig.Instance.UsingStatus.ClampPrecisionDelayBeforeInArmUp);
            //        MultiCylinderOperate(new List<Cylinder>() { _upPositionCylinder1, _upPositionCylinder2 },
            //                             5000,
            //                             new List<bool>() { onoff, onoff });
            //        if (isDelay)
            //            Thread.Sleep(SystemConfig.Instance.UsingStatus.ClampPrecisionDelayInArmUp);
            //    }
            //    else
            //    {
            //        if (isDelay)
            //            Thread.Sleep(SystemConfig.Instance.UsingStatus.ClampPrecisionDelayBeforeInArmUp);
            //        MultiCylinderOperate(new List<Cylinder>() { _downPositionCylinder1, _downPositionCylinder2 },
            //                             5000,
            //                             new List<bool>() { onoff, onoff });
            //        if (isDelay)
            //            Thread.Sleep(SystemConfig.Instance.UsingStatus.ClampPrecisionDelayInArmUp);
            //    }
            //}
        }

        private void ClearDoneReleaseTray()
        {

        }
    }
    public enum HeaderPosition
    {
        RobotSide,
        ArmSide,
    }
}
